package com.yuanjin.activity;

import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.yuanjin.R;
import com.yuanjin.adapter.FriendsShowAdapter;
import com.yuanjin.models.FriendsShowItem;
import com.yuanjin.util.MediaUtils;
import com.yuanjin.util.MyNetWorkUtils;
import com.yuanjin.util.Yuanjin;

public class FriendsCircleActivity extends Activity implements OnClickListener {
	private ListView friendshowListView;
	private String srcPath = "/sdcard/testimg.jpg";
	private ImageButton topbar_rightImageButton;
	private SharedPreferences sp;
	private String username;
	private FriendsShowAdapter fsAdapter;
	Vector<FriendsShowItem> list = Yuanjin.friendsShowItemList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friendscircle);

		friendshowListView = (ListView) findViewById(R.id.friendshowListView);
		// topbar_rightButton.setOnClickListener(this);

		topbar_rightImageButton = (ImageButton) findViewById(R.id.topbar_rightImageButton);
		topbar_rightImageButton.setOnClickListener(this);
		topbar_rightImageButton.setVisibility(View.VISIBLE);

		sp = getSharedPreferences(Yuanjin.YuanjinSharedPreference,
				Activity.MODE_PRIVATE);
		// downloadButton.setOnClickListener(this);

		/**********************************************************/
		friendshowdataInit();
		fsAdapter = new FriendsShowAdapter(list, this);
		friendshowListView.setAdapter(fsAdapter);

		/************ 测试 *************/
		MyNetWorkUtils mnwu = new MyNetWorkUtils();
		mnwu.downloadFile(MyNetWorkUtils.FriendsShow, handler);
	}

	// Vector<FriendsShowItem> list = new Vector<FriendsShowItem>();
	

	public void friendshowdataInit() {
		FriendsShowItem fsi = new FriendsShowItem("欢迎",
				"you can say something here", null);
		list.add(fsi);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Yuanjin.ALBUM_OK && data != null) {
			Uri uri = data.getData();
			Log.e("test", "ceshi-------------->> " + uri.toString());
			ContentResolver cr = this.getContentResolver();
			// 获得选中图片的绝对路径
			srcPath = MediaUtils.getImageAbsolutePath(this, uri);
			Log.e("MyDetailInfoActivity", "获得图片的绝对路径为 ：" + srcPath);

			Intent intent = new Intent(this, FriendShowUploadActivity.class);
			intent.putExtra("srcPath", srcPath);
			// startActivity(intent);
			startActivityForResult(intent, 1001);

		}
		if (requestCode==1001) {
			fsAdapter.notifyDataSetChanged();
		}
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.arg1) {
			case 1000:// 朋友圈 json 数据
				String jsonString = (String) msg.obj;
				Log.e("test", "------得到最终返回的数据---->" + jsonString);

				// ///////////////json parse///////////////////
				try {
					JSONArray jsonArray = (JSONArray) new JSONTokener(
							jsonString).nextValue();
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject jsonObject = (JSONObject) jsonArray.get(i);
						String fname = jsonObject.getString("friendname");
						String message = jsonObject.getString("message");
						JSONArray jArray = jsonObject.getJSONArray("pictures");
						FriendsShowItem fsi = new FriendsShowItem(fname,
								message, jArray);
						list.add(fsi);

						Log.e("testing", "-----fname--->" + fname);
						Log.e("testing", "-----message--->" + message);
						for (int j = 0; j < jArray.length(); j++) {
							String imgpath = (String) jArray.get(j);
							Log.e("testing", "-----imgpath-" + j + "-->"
									+ imgpath);
						}
					}
					fsAdapter.notifyDataSetChanged();

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// //////////////////////////////////

				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	/**
	 * @param inputString
	 *            json
	 */
	public void paseJsonInfo(String inputString) {
		try {
			JSONObject object = (JSONObject) new JSONTokener(inputString)
					.nextValue();// 将字符串转成jsonobject
			String name = object.getString("name");
			String value = object.getString("value");
			JSONArray pictureslink = object.getJSONArray("pictureslink");

			Log.e("testing", "------------result is =  ------------->>"
					+ "name = " + name + " value = " + value);
			Log.e("testing",
					"------------pictureslink.get(0); is =  ------------->>"
							+ pictureslink.get(0));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.topbar_rightImageButton:

			Intent intent = new Intent(Intent.ACTION_PICK, null);
			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					"image/*");
			startActivityForResult(intent, Yuanjin.ALBUM_OK);
			break;

		default:
			break;
		}
	}

}
